package com.ruoyi.system.task;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.ruoyi.system.domain.SysCertificateInfo;
import com.ruoyi.system.mapper.SysCertificateInfoMapper;
import com.ruoyi.system.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @ClassName: CertificateStateCheck
 * @Description: 服务器启动检查所有证书状态并更新已失效证书状态
 *
 * @author linjinkun
 * @date 2023/10/24
 */
@Component
public class CertificateStateCheck implements ApplicationRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(DruidDataSourceAutoConfigure.class);

    @Resource
    private SysCertificateInfoMapper sysCertificateInfoMapper;

    @Override
    public void run(ApplicationArguments args){
        Date date = new Date();
        date = DateUtils.getMinusDay(date, 1);
        System.out.println(date);
        //往前推一天是失效日期
        SysCertificateInfo sysCertificateInfo = new SysCertificateInfo();
//        sysCertificateInfo.setCertificateDeadline(sqlDate);
        List<SysCertificateInfo> list = sysCertificateInfoMapper.selectSysCertificateInfoList(sysCertificateInfo);
        //对这些证书的state字段进行修改
        for (SysCertificateInfo i : list) {
            if (date.after(i.getCertificateDeadline()) && "有效".equals(i.getCertificateCheckStatus())) {
                i.setCertificateCheckStatus("失效");
                sysCertificateInfoMapper.updateSysCertificateInfo(i);
            }
        }
        LOGGER.info("有效性验证完毕");
    }
}
